/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.server.login;

import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthHelper;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthParameters;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import com.google.orkut.client.api.samples.greetings.server.CachedConfig;
import com.google.orkut.client.api.samples.greetings.server.Constants;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;

import java.security.SecureRandom;
import java.util.Random;

/**
 * Guice bindings for authentication.
 *
 * @author Shishir Birmiwal
 */
public class AuthModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(Random.class).toInstance(new SecureRandom());
  }

  @Provides
  OAuthHelper getOAuthHelper(OAuthHmacSha1Signer signer) {
    return new GoogleOAuthHelper(signer);
  }

  @Provides
  @Named("consumerKey")
  String getConsumerKey(CachedConfig config) {
    return config.getConfig().getConsumerKey();
  }

  @Provides
  @Named("consumerSecret")
  String getConsumerSecret(CachedConfig config) {
    return config.getConfig().getConsumerSecret();
  }

  @Provides
  OAuthConsumer getOAuthConsumer(@Named("consumerKey") String key,
      @Named("consumerSecret") String secret) {
    return new DefaultOAuthConsumer(key, secret);
  }

  @Provides
  OAuthParameters getOAuthParams(@Named("consumerKey") String key,
      @Named("consumerSecret") String secret) {
    GoogleOAuthParameters params = new GoogleOAuthParameters();
    params.setOAuthConsumerKey(key);
    params.setOAuthConsumerSecret(secret);
    params.setScope(Constants.SCOPE);
    return params;
  }
}
